﻿using System;

namespace UniRx.Operators
{
	// Token: 0x0200032B RID: 811
	internal class TimerObservable : OperatorObservableBase<long>
	{
		// Token: 0x060010DF RID: 4319 RVA: 0x0004DBA8 File Offset: 0x0004BFA8
		public TimerObservable(DateTimeOffset dueTime, TimeSpan? period, IScheduler scheduler) : base(scheduler == Scheduler.CurrentThread)
		{
			this.dueTimeA = new DateTimeOffset?(dueTime);
			this.period = period;
			this.scheduler = scheduler;
		}

		// Token: 0x060010E0 RID: 4320 RVA: 0x0004DBD2 File Offset: 0x0004BFD2
		public TimerObservable(TimeSpan dueTime, TimeSpan? period, IScheduler scheduler) : base(scheduler == Scheduler.CurrentThread)
		{
			this.dueTimeB = new TimeSpan?(dueTime);
			this.period = period;
			this.scheduler = scheduler;
		}

		// Token: 0x060010E1 RID: 4321 RVA: 0x0004DBFC File Offset: 0x0004BFFC
		protected override IDisposable SubscribeCore(IObserver<long> observer, IDisposable cancel)
		{
			//TimerObservable.<SubscribeCore>c__AnonStorey0 <SubscribeCore>c__AnonStorey = new TimerObservable.<SubscribeCore>c__AnonStorey0();
			//<SubscribeCore>c__AnonStorey.$this = this;
			//<SubscribeCore>c__AnonStorey.timerObserver = new TimerObservable.Timer(observer, cancel);
			//TimeSpan timeSpan = (this.dueTimeA == null) ? this.dueTimeB.Value : (this.dueTimeA.Value - this.scheduler.Now);
			//if (this.period == null)
			//{
			//	return this.scheduler.Schedule(Scheduler.Normalize(timeSpan), delegate()
			//	{
			//		<SubscribeCore>c__AnonStorey.timerObserver.OnNext();
			//		<SubscribeCore>c__AnonStorey.timerObserver.OnCompleted();
			//	});
			//}
			//TimerObservable.<SubscribeCore>c__AnonStorey1 <SubscribeCore>c__AnonStorey2 = new TimerObservable.<SubscribeCore>c__AnonStorey1();
			//<SubscribeCore>c__AnonStorey2.periodicScheduler = (this.scheduler as ISchedulerPeriodic);
			//if (<SubscribeCore>c__AnonStorey2.periodicScheduler == null)
			//{
			//	TimeSpan timeP = Scheduler.Normalize(this.period.Value);
			//	return this.scheduler.Schedule(Scheduler.Normalize(timeSpan), delegate(Action<TimeSpan> self)
			//	{
			//		<SubscribeCore>c__AnonStorey.timerObserver.OnNext();
			//		self(timeP);
			//	});
			//}
			//if (timeSpan == this.period.Value)
			//{
			//	return <SubscribeCore>c__AnonStorey2.periodicScheduler.SchedulePeriodic(Scheduler.Normalize(timeSpan), new Action(<SubscribeCore>c__AnonStorey.timerObserver.OnNext));
			//}
			//SerialDisposable disposable = new SerialDisposable();
			//disposable.Disposable = this.scheduler.Schedule(Scheduler.Normalize(timeSpan), delegate()
			//{
			//	<SubscribeCore>c__AnonStorey.timerObserver.OnNext();
			//	TimeSpan timeSpan2 = Scheduler.Normalize(<SubscribeCore>c__AnonStorey.$this.period.Value);
			//	disposable.Disposable = <SubscribeCore>c__AnonStorey2.periodicScheduler.SchedulePeriodic(timeSpan2, new Action(<SubscribeCore>c__AnonStorey.timerObserver.OnNext));
			//});
			//return disposable;
			return null;
		}

		// Token: 0x040009F2 RID: 2546
		private readonly DateTimeOffset? dueTimeA;

		// Token: 0x040009F3 RID: 2547
		private readonly TimeSpan? dueTimeB;

		// Token: 0x040009F4 RID: 2548
		private readonly TimeSpan? period;

		// Token: 0x040009F5 RID: 2549
		private readonly IScheduler scheduler;

		// Token: 0x0200032C RID: 812
		private class Timer : OperatorObserverBase<long, long>
		{
			// Token: 0x060010E2 RID: 4322 RVA: 0x0004DD9F File Offset: 0x0004C19F
			public Timer(IObserver<long> observer, IDisposable cancel) : base(observer, cancel)
			{
			}

			// Token: 0x060010E3 RID: 4323 RVA: 0x0004DDAC File Offset: 0x0004C1AC
			public void OnNext()
			{
				try
				{
					IObserver<long> observer = this.observer;
					long value;
					this.index = (value = this.index) + 1L;
					observer.OnNext(value);
				}
				catch
				{
					base.Dispose();
					throw;
				}
			}

			// Token: 0x060010E4 RID: 4324 RVA: 0x0004DDF8 File Offset: 0x0004C1F8
			public override void OnNext(long value)
			{
			}

			// Token: 0x060010E5 RID: 4325 RVA: 0x0004DDFC File Offset: 0x0004C1FC
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x060010E6 RID: 4326 RVA: 0x0004DE34 File Offset: 0x0004C234
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x040009F6 RID: 2550
			private long index;
		}
	}
}
